package com.bw.educate.controller;


import com.bw.educate.common.ResponseResult;
import com.bw.educate.entity.EduCategoryChild;
import com.bw.educate.entity.EduCourseChapter;
import com.bw.educate.entity.EduCourseSection;
import com.bw.educate.entity.EduRes;
import com.bw.educate.service.EduResService;
import com.bw.educate.service.IEduCourseChapterService;
import com.bw.educate.service.IEduCourseSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("chapter")
public class EduCourseChapterController {

    @Autowired
    private IEduCourseChapterService courseChapterService;

    @Autowired
    private IEduCourseSectionService courseSectionService;


    /**
     * @Description:保存
     * @Author: 李闯
     * @Date: 2020/8/18 20:15
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("save")
    public ResponseResult addSave(@RequestBody EduCourseSection courseSection){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            if(courseSection.getId()==null){
                courseSection.setUpdateTime(new Date());
                courseSectionService.saveOrUpdate(courseSection);
            }else{
                courseSectionService.updateById(courseSection);
            }
            result.setMessage("保存成功");

        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("保存失败"+e.toString());
        }

        return result;
    }

    /**
     * @Description:获取章节树
     * @Author: 李闯
     * @Date: 2020/8/18 20:15
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("getTree/{id}")
    public ResponseResult getTree(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();
        try {
            result.setResult(courseChapterService.getTree(id));
            result.setMessage("成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("失败"+e.toString());
        }

        return result;
    }


    /**
     * @Description:根据id获取章节
     * @Author: 李闯
     * @Date: 2020/8/18 20:15
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("get/{id}")
    public ResponseResult get(@PathVariable("id") Long id) {//回显菜单
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EduCourseSection courseSection = courseSectionService.getById(id);
            result.setResult(courseSection);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询失败:" + e.toString());
        }
        return result;
    }

    /**
     * @Description:根据id删除章
     * @Author: 李闯
     * @Date: 2020/8/18 20:16
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("delTree/{id}")
    public ResponseResult delTree(@PathVariable("id") Long id) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            //删除的逻辑，两条sql
            courseChapterService.delTree(id);
            result.setMessage("删除成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("删除失败:" + e.toString());
        }
        return result;
    }

}
